app.controller('registerBaseInfoCtrl',['$scope','$stateParams','$state','$cookies','toaster','registerService','$interval',
    function($scope,$stateParams,$state,$cookies,toaster,registerService,$interval){

        var state=$stateParams.state;

        //阅读协议
        $scope.readProtocol=function(){
            if(!$scope.baseinfo.checkbox){
                return;
            }

            $scope.isRead=true;
        }

        $scope.baseinfo={
            gender:0,
            time:new Date().getTime()
        };

        $scope.baseinfoForm = function(){
            var registerToken = "mobilePhone="+$scope.baseinfo.phone+"&code="+$scope.baseinfo.code+"&time="
                +$scope.baseinfo.time+"&password="+$scope.baseinfo.password+"&name="+$scope.baseinfo.name+"&email="+$scope.baseinfo.email
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);
            $scope.baseinfo.registerToken = encodeURIComponent(encrypt.encrypt(registerToken));
            $scope.baseinfo.type = 0;
            $scope.baseinfo.method = 0;
            if(state==2){
                debugger;
                $scope.baseinfo.isChain= 0;
                $scope.baseinfo.isPlatform= 1;
            }else{
                $scope.baseinfo.isChain=state;
                $scope.baseinfo.isPlatform= 0;
            }
           
            registerService.shopRegisterBaseinfo($scope).then(function(result){
                switch (result.status){
                    case 200:
                        var obj = result.data;
                        delete obj["logoutTime"];
                        for(var key in obj){
                            setCookie(key,[obj[key]]);
                        }

                        if(state==0){
                            $state.go('access.register.shop',{'state':state});
                        }else if(state==1){
                            $state.go('access.register.chain',{'state':state});
                        }else if(state==2){
                            $state.go('access.register.shop',{'state':state});
                        }
                        break;
                    case 602:
                    case 408:
                    case 647:
                    case 648:
                        $scope.status=result.status;
                        break;
                    default :
                        alert("发生未知错误！请联系管理员");
                        break
                }
            });
        };

        //检查手机号码是否被使用
        $scope.getCode = function(){
            if(!angular.isUndefined($scope.baseinfo.phone)) {
                registerService.checkPhone($scope).then(function (result) {
                    switch (result.status){
                        case 200:
                            $scope.sendSms();
                            break;
                        case 602:
                            alert("手机号码已被注册,请找登录");
                            break;
                        default :
                            alert("发生未知错误！请联系管理员");
                            break
                    }
                });
            }
        };

        $scope.description = "获取验证码";
        $scope.sendSms = function(){
            $scope.canClick=true;
            registerService.getMobilePhoneCode($scope).then(function(result){
                switch (result.status){
                    case 200:
                        timer();
                        break;
                    case 645:
                        alert(result.message);
                    case 680:
                        $scope.canClick=false;
                        alert("不能频繁发送验证码,请稍后再发");
                        break;
                    default :
                        $scope.canClick=false;
                        alert("发生未知错误！请联系管理员");
                        break
                }
            });
        }

        //时间定时器
        var timerHandler;
        var second=59;
        var timer=function(){
            timerHandler =$interval(function(){
                if(second<=0){
                    $interval.cancel(timerHandler);    //当执行的时间59s,结束时，取消定时器 ，cancle方法取消
                    second=59;
                    $scope.description="获取验证码";
                    $scope.canClick=false;    //因为 ng-disabled属于布尔值，设置按钮可以点击，可点击发送
                }else{
                    $scope.description=second+"s后重发";
                    second--;
                    $scope.canClick=true;
                }
            },1000)
        }


        var setCookie=function(name, value) {
            //path=/表示全站有效，而不是当前页
            document.cookie = name + "=" + value + ";path="+$scope.app.cookiePath;
        }


}]);

//套餐
app.controller('mealCtrl',['$scope','registerService','toaster','$state','$stateParams', function($scope,registerService,toaster,$state,$stateParams){

    $scope.setMealVO={
        type:$stateParams.state
    };

    //查询套餐信息
    $scope.loading=false;

    $scope.getSetMealList = function(){
        if($scope.loading){
            return;
        }
        $scope.loading=true;

        registerService.getSetMealList($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.loading=false;
                    $scope.setMeals = result.data;
                    break;
                default :
                    $scope.loading=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });
    }

    $scope.getSetMealList();

    //选择套餐
    //单店铺套餐
    $scope.addSetShopMeal = function(setMealId){
        $scope.setMeal = setMealId;
        registerService.addSetShopMeal($scope).then(function(result){
            switch (result.status){
                case 200:
                    $state.go('access.login');
                    break;
                case 601:
                case 624:
                    alert("注册失败，请重新注册");
                    break;
                case 623:
                    alert("选择行业有误!请重新选择");
                    break;
                default :
                    $state.go('access.login');
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    //连锁套餐
    $scope.addSetChainMeal = function(setMealId){
        $scope.setMeal = setMealId;
        registerService.addSetChainMeal($scope).then(function(result){
            switch (result.status){
                case 200:
                    $state.go('access.login');
                    break;
                case 601:
                case 624:
                    alert("注册失败，请重新注册");
                    break;
                case 623:
                    alert("选择行业有误!请重新选择");
                    break;
                default :
                    $state.go('access.login');
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }


    $scope.addSetMeal=function(id,index){
        $scope.setMealId=id;
        $scope.index=index;
    }

    $scope.submitSetMeal=function(){
        if($scope.setMealVO.type==0){
            $scope.addSetShopMeal();
        }else if($scope.setMealVO.type==1){
            $scope.addSetChainMeal();
        }
    }




}]);



//创建店铺
app.controller('registerShopCtrl',['$scope','$state','$stateParams','$cookies','registerService','toaster','$location',
    function($scope,$state,$stateParams,$cookies,registerService,toaster,$location){

        $state.state = $stateParams.state;

        registerService.getIndustryFullName($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.industry=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });


        $scope.addState=false;

        $scope.registerShopAdd = function(){

            if($scope.addState){
                return;
            }

            $scope.addState=true;
            if($state.state==2){
                $scope.shop.isPlatform= 1;
            }

            registerService.registerShopAdd($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.addState=false;
                        $state.go('access.register.cost',{state:0});
                        break;
                    case 601:
                    case 622:
                        alert("注册失败，请重新注册");
                        $scope.addState=false;
                        break;
                    case 616:
                        alert("账号已存在，请登录");
                        $scope.addState=false;
                        break;
                    default :
                        $scope.addState=false;
                        $state.go('access.login');
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }

            });
        }

        $scope.shop={};
        $scope.selectCounty=null;
        $scope.selectTown=null;
        $scope.selectProvice="省";
        $scope.selectTown="市";
        $scope.selectCounty="区";
        $scope.showState=false;
        $scope.towns=[];
        $scope.countys=[];
        $scope.currentState=0;
        $scope.steps={percent:20, step1:true, step2:false, step3:false};

        //点击选择框执行的方法
        $scope.findProvinces=function(level,parentId){
            $scope.showState=!$scope.showState;
            if($scope.showState){
                $scope.showState=true;
                $scope.findArea(level,parentId);
            }
        };
        //点击省之后执行的方法
        $scope.findTown=function(level,item){
            $scope.findArea(level,item.cityId,item.name);
            $scope.shop.areaProvinceId=item.id;
        };
        //点击市之后执行的方法
        $scope.findCounty=function(level,item){
            $scope.findArea(level,item.cityId,item.name);
            $scope.shop.areaCityId=item.id;
        };
        //点击区之后执行的方法
        $scope.CountyHide=function(item){
            $scope.selectCounty=item.name;
            $scope.shop.areaCountyId=item.id;
            $scope.showState=false;
            $scope.currentState=3;
        };



        //发送并处理service传回来的数据
        $scope.findArea=function(level,parentId,name){
            registerService.findArea($scope,level,parentId,name).then(function(result){
                switch (result.status){
                    case 200:
                        //请求level=0时，返回的是省的区域
                        if(level==0){
                            $scope.provinces=result.data;
                        //请求level=1时，返回的是市的区域
                        }else if(level==1){
                            $scope.towns=result.data;
                            $scope.selectProvice=name;
                            $scope.currentState=1;
                            $scope.selectTown="市";
                            $scope.selectCounty="区";
                            $scope.steps.step2=true;
                        }else if(level==2){
                        //请求level=2时，返回的是区的区域
                            $scope.countys=result.data;
                            $scope.selectTown=name;
                            $scope.steps.step3=true;
                            $scope.currentState=2;
                            $scope.selectCounty="区";
                        }
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
}]);




//注册连锁店
app.controller('registerChainCtrl',['$scope','$state','$cookies','registerService','toaster','$location',
    function($scope,$state,$cookies,registerService,toaster,$location){

        //查询行业商品
        $scope.getIndustryFullName=function(){
            registerService.getIndustryFullName($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.industry=result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }

        $scope.getIndustryFullName();

        //添加连锁信息
        $scope.chain={
            count:1
        };

        $scope.addState=false;

        $scope.addChain=function(){

            if($scope.addState){
                return;
            }

            $scope.addState=true;

            registerService.addChain($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.addState=false;
                        toaster.pop('success', "提示", "操作成功");
                        $state.go('access.register.cost',{state:1});
                        break;
                    case 695:
                        alert(result.message);
                        $scope.addState=false;
                        break;
                    default :
                        $scope.addState=false;
                        $state.go('access.login');
                        break;
                }
            })
        }


}]);

app.controller('registerBusinessCtrl',['$scope','$state','registerService','toaster',function($scope,$state,registerService,toaster){
    $scope.path=$scope.app.image.shopinfo;

    $scope.frontLimit=1;
    $scope.frontImages=new Array();
    $scope.frontImgQueue=new Array();

    $scope.backLimit=1;
    $scope.backImages=new Array();
    $scope.backQueue=new Array();

    $scope.userLimit=1;
    $scope.userImages=new Array();
    $scope.userQueue=new Array();


    $scope.subjectLimit=1;
    $scope.subjectImages=new Array();
    $scope.subjectQueue=new Array();


    $scope.shopInfo={
        identityType:0,
        subjectType:0
    };

    $scope.imgType=-1;

    $scope.delFrontImage=function(){
        $scope.imgType=0;
        $scope.delImg();
    }

    $scope.delBackImage=function(){
        $scope.imgType=1;
        $scope.delImg();
    }

    $scope.delUserImage=function(){
        $scope.imgType=2;
        $scope.delImg();
    }

    $scope.delSubjectImage= function () {
        $scope.imgType=3;
        $scope.delImg();
    }

    $scope.isDel=false;

    $scope.delImg=function(){
        if($scope.isDel){
            return;
        }

        $scope.isDel=true;

        registerService.delImage($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.isDel=false;
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findByShopId();
                    break;
                default :
                    $scope.isDel=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.init=function(){
        $scope.frontLimit=1;
        $scope.frontImages=new Array();

        $scope.backLimit=1;
        $scope.backImages=new Array();

        $scope.userLimit=1;
        $scope.userImages=new Array();


        $scope.subjectLimit=1;
        $scope.subjectImages=new Array();
    }

    $scope.findByShopId=function(){
        registerService.findByShopId($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.init();
                    $scope.shopInfo=result.data;

                    if($scope.shopInfo.identityBackImgUrl!=null&& !angular.isUndefined($scope.shopInfo.identityBackImgUrl)){
                        $scope.backImages.push($scope.shopInfo.identityBackImgUrl);
                        $scope.backLimit=0;
                    }

                    if($scope.shopInfo.identityFrontImgUrl!=null && !angular.isUndefined($scope.shopInfo.identityFrontImgUrl)){
                        $scope.frontImages.push($scope.shopInfo.identityFrontImgUrl);
                        $scope.frontLimit=0;
                    }

                    if($scope.shopInfo.identityImgUrl!=null && !angular.isUndefined($scope.shopInfo.identityImgUrl)){
                        $scope.userImages.push($scope.shopInfo.identityImgUrl);
                        $scope.userLimit=0;
                    }

                    if($scope.shopInfo.subjectImgUrl!=null && !angular.isUndefined($scope.shopInfo.subjectImgUrl)){
                        $scope.subjectImages.push($scope.shopInfo.subjectImgUrl);
                        $scope.subjectLimit=0;
                    }

                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.findByShopId();


    $scope.updateShopBusiness = function () {

        if($scope.backQueue.length>0){
            $scope.shopInfo.identityBackImgUrl=$scope.backQueue[0].fileName;
        }else{
            delete $scope.shopInfo.identityBackImgUrl;
        }

        if($scope.frontImgQueue.length>0){
            $scope.shopInfo.identityFrontImgUrl=$scope.frontImgQueue[0].fileName;
        }else{
            delete $scope.shopInfo.identityFrontImgUrl;
        }

        if($scope.userQueue.length>0){
            $scope.shopInfo.identityImgUrl=$scope.userQueue[0].fileName;
        }else{
            delete $scope.shopInfo.identityImgUrl;
        }

        if($scope.subjectQueue.length>0){
            $scope.shopInfo.subjectImgUrl=$scope.subjectQueue[0].fileName;
        }else{
            delete $scope.shopInfo.subjectImgUrl;
        }

        delete $scope.shopInfo.businessTerm;

        registerService.updateShopBusiness($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $state.go('access.register.cost',{state:0});
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }


    //点击图片时放大显示图片
    $scope.changePic=function($event){
        var img=$event.srcElement || $event.target;
        angular.element(".bigimage")[0].src=img.src;
        angular.element(".js-imgview")[0].style.display="block";
        angular.element(".js-imgview-mask")[0].style.display="block";
    }
    //点击图片时放小显示图片
    $scope.closePic =function(){
        angular.element(".js-imgview")[0].style.display="none";
        angular.element(".js-imgview-mask")[0].style.display="none";

    }

}])

app.controller('registerImproveCtrl',['$scope','$state','$stateParams','registerService','toaster',function($scope,$state,$stateParams,registerService,toaster){
    $scope.id=$stateParams.id;

    $scope.findSystemUser=function(){
        registerService.findSystemUser($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.systemUser=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.findSystemUser();


}])

